【笔记】SwaggerHack学习笔记

前言

批量测试Swagger中出现的API接口

下载项目

1
2
git clone https://github.com/jayus0821/swagger-hack.git
cd swagger-hack

源代码

swagger-hack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import json
import time
import requests
import csv,numpy.compat.setup



def banner():
print('''

_ _
_____ ____ _ __ _ __ _ ___ _ __ | |__ __ _ ___| | __
/ __\ \ /\ / / _` |/ _` |/ _` |/ _ \ '__|____| '_ \ / _` |/ __| |/ /
\__ \\ V V / (_| | (_| | (_| | __/ | |_____| | | | (_| | (__| <
|___/ \_/\_/ \__,_|\__, |\__, |\___|_| |_| |_|\__,_|\___|_|\_\\
|___/ |___/
by jayus
''')

def get_specs(url):#获取标准列表
specs_url = url + "/swagger-resources"
res = requests.get(url = specs_url)
#print(res.text)
specs = json.loads(res.text)
# for spec in specs:
# print(spec)#{'name': 'kt-research-biz', 'url': '/research/v2/api-docs', 'swaggerVersion': '2.0', 'location': '/research/v2/api-docs'}
return specs

def check_spec(spec_url,url,f):#前一个是接口文档,用于分析,后一个是文档对应的实际接口请求地址
res = requests.get(url = spec_url)
try:
paths = json.loads(res.text)['paths']
print("[+] : 此标准下共有 %d 个接口"%(len(paths)))
except:
print("此标准为空")
return 0

for path in paths:
print("[+] : 开始测试接口 %s " %(path))
methods = paths[path]
for method in methods:
#print("接口请求方式: ",method)#get/post/put...
#print( paths[path][method])#对应method的配置信息
tags = paths[path][method]['tags'][0]
summary = paths[path][method]['summary']
#print("接口summary: ",summary)
operationId = paths[path][method]['operationId']
if 'consumes' in paths[path][method].keys():#json格式
consumes = paths[path][method]['consumes'][0]
#print(consumes)
else:
consumes = '0'

if consumes != '0':#如果是json格式传输 post/put #post和post都是发送的json,但是接口文档并没有如何构造json的参数,目前只是随便发送一个
#print("使用json格式传输")
# json_array = {}
# if 'parameters' in paths[path][method]:
# parameters = paths[path][method]['parameters']
# print("接口参数个数为 %d"%(len(parameters)))
# for parameter in parameters:
# #print(parameter)
# if parameter['type'] == "boolean":#布尔型全为true,string和数字全部为1
# json_array[parameter['name']] = 'true'
# else:
# json_array[parameter['name']] = '1'
# else:
# json_array = ''
# print("接口参数个数为 %d"%(0))

# print("构造请求参数...")
# json_string = json.dumps(json_array)
# print(json_string)

json_string = '''{
"contractNumber": "string",
"createdBy": "string",
"createdTime": "2021-02-01T09:33:58.398Z",
"cutoffDate": "2021-02-01T09:33:58.398Z",
"delFlag": "string",
"dispatchForm": "string",
"dispatchUnit": "string",
"effectDate": "2021-02-01T09:33:58.398Z",
"fileList": "string",
"id": 0,
"makeDate": "2021-02-01T09:33:58.398Z",
"manageMethod": "string",
"name": "string",
"peopleNumber": "string",
"remark": "string",
"title": "string",
"updatedBy": "string",
"updatedTime": "2021-02-01T09:33:58.398Z"
}'''


if method == "post":
res = requests.post(url = url + path , data = json_string)
elif method == "put":
res = requests.put(url = url + path , data = json_string)
# print(method)
# print(url + path)
# print(res.status_code)
try:#post居然也可能没参数
row = [spec_url,summary,path,method,consumes,url + path,str(len(paths[path][method]['parameters'])),json_string,res.status_code,res.text]
except:
row = [spec_url,summary,path,method,consumes,url + path,'0',json_string,res.status_code,res.text]
writer.writerow(row)


else:#不是json传输
# print("不使用json格式传输")
if "{" in path:
# print("parameter in url")
parameter = paths[path][method]['parameters'][0]
try:
if parameter['type'] == "boolean":#布尔型全为true,string和数字全部为1
tmp = "true"
else:
tmp = "1"
except:
# print("no type")
tmp = "{1}"
if method == 'get':
res = requests.get(url = url + path[:path.index('{')] + tmp)
# print(method)
elif method == 'delete':
res = requests.delete(url = url + path[:path.index('{')] + tmp)
# print(method)
# print(url + path[:path.index('{')] + tmp)
# print(res.status_code)

row = [spec_url,summary,path,method,consumes,url + path[:path.index('{')],str(len(paths[path][method]['parameters'])),"",res.status_code,res.text]
writer.writerow(row)

else:
query_string = ''
if 'parameters' in paths[path][method]:
parameters = paths[path][method]['parameters']
num_of_param = len(parameters)
# print("接口参数个数为 %d"%(len(parameters)))
for parameter in parameters:
#print(parameter)
try:
if parameter['type'] == "boolean":#布尔型全为true,string和数字全部为1
query_string += "&%s=true"%(parameter['name'])
else:
query_string += "&%s=1"%(parameter['name'])
except:
# print("no type...")
query_string += "&%s={1}"%(parameter['name'])
else:
query_string = ''
num_of_param = 0
# print("接口参数个数为 %d"%(0))
# print("构造请求参数...")
query_string = query_string[1:]
# print(query_string)

if method == "get":
res = requests.get(url = url + path + "?" + query_string)
# print(method)
elif method == "delete":
res = requests.delete(url = url + path + "?" + query_string)
# print(method)
# print(url + path + query_string)
# print(res.status_code)

row = [spec_url,summary,path,method,consumes,url + path + "?" + query_string,str(num_of_param),"",res.status_code,res.text]
writer.writerow(row)




# print("================")
# print()

time.sleep(0)


if __name__ == '__main__':
banner()
url = "https://xx.xx.xx"#eg:https://xx.xx.xx/swagger-ui.html 只取前面host部分
specs = get_specs(url)
print("[+] 共抓取到 %d 个标准"%(len(specs)))

f = open('swagger.csv','w',newline='',encoding='utf-8')#写到csv中
writer = csv.writer(f)
try:
writer.writerow(["标准","summary","path","method","consumes","url","num of params","data","status_code","response"])
except Exception as e:
print(e)

for spec in specs:
spec_url = url + spec['url']
pre = spec['url'].split('/')[1]
print("[+] : 开始测试 %s 标准"%(spec_url))
check_spec(spec_url,url + "/" + pre,f)
#break
swagger-hack2.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
import json,os,re
import time, sys
from urllib.parse import urlparse
import requests
import csv
import argparse
from multiprocessing import Pool, Manager
requests.packages.urllib3.disable_warnings()


from loguru import logger
logger.remove()
handler_id = logger.add(sys.stderr, level="DEBUG")#设置输出级别




payload_array = {"string":"1","boolean":"true","integer":"1","array":"1","number":"1","object":""} #根据参数类型进行赋值

#post类型就发送这个数据
json_payload = """{
"code": "string",
"createTime": "2021-02-05T10:34:37.691Z",
"delFlag": "string",
"deptId": 0,
"fullName": "string",
"fullPathCode": "string",
"fullPathName": "string",
"isVirtual": true,
"name": "string",
"outCode": "string",
"outParentCode": "string",
"parentCode": "string",
"parentId": 0,
"parentName": "string",
"sort": 0,
"updateTime": "2021-02-05T10:34:37.691Z"
}"""

def banner():
logger.info('''

_ _
_____ ____ _ __ _ __ _ ___ _ __ | |__ __ _ ___| | __
/ __\ \ /\ / / _` |/ _` |/ _` |/ _ \ '__|____| '_ \ / _` |/ __| |/ /
\__ \\ V V / (_| | (_| | (_| | __/ | |_____| | | | (_| | (__| <
|___/ \_/\_/ \__,_|\__, |\__, |\___|_| |_| |_|\__,_|\___|_|\_\\
|___/ |___/
by jayus


python swagger.py -h
---------------------------------------------------------------------
''')


def check(url):
try:
res = requests.get(url = url, timeout = 5, verify = False)
if "<html" in res.text:
logger.debug("[+] 输入url为swagger首页,开始解析api文档地址")
return 3 #html
elif "\"parameters\"" in res.text:
logger.debug("[+] 输入url为api文档地址,开始构造请求发包")
return 2 #api doc
elif "\"location\"" in res.text:
logger.debug("[+] 输入url为resource地址,开始解析api文档地址")
return 1 #source
except KeyboardInterrupt:
print("kill")
except Exception as e:
print(e)
return 0


def savedata(filename):
if ".csv" in filename:
pass
elif ".txt" in filename:
pass


def get_api_docs_pathes(resource_url):#输入resource,解析出各api文档的url
domain = urlparse(resource_url)
domain = domain.scheme + "://" + domain.netloc
try:
res = requests.get(url = resource_url, verify = False, timeout = 10)
resources = json.loads(res.text)
except Exception as e:
print(e)
return []

pathes = []
if isinstance(resources,tuple):
if "apis" in resources.keys():#版本不同,格式不一样
for api_docs in resources['apis']:
pathes.append(domain + api_docs['path'])
return pathes
else:
for i in resources:
pathes.append(domain + i['location'])
return pathes






def go_source(url):
pass




def go_docs(url,global_data):
try:
domain = urlparse(url)
domain = domain.scheme + "://" + domain.netloc
try:
res = requests.get(url = url, timeout = 5, verify = False)
except:
logger.error("timeout...")
res = json.loads(res.text)
basePath = ''
if "basePath" in res.keys():
basePath = res['basePath'] #eg:/santaba/rest
elif "servers" in res.keys():
basePath = res["servers"]['url']
else:
basePath = ''
paths = res['paths']
path_num = len(paths)
logger.info("[+] {} has {} paths".format(url,len(paths)))
for path in paths:#path字符串
res_path = path
logger.debug("test on {} => {}".format(url,path))
try:
for method in paths[res_path]:#get/post/字符串
path = res_path
text = str(paths[path][method])
param_num = text.count("'in':")
try:
summary = paths[path][method]['summary']
except:
summary = path
if method == 'post' or method == 'put':#这两种请求,参数如何构造在接口文档中没有,暂时不知道在哪找,所以随便发一个包
#post分没参数和有参数两种,没参数直接随便post个json,有参数但是in body还是json
#有参数但是in path 就判断类型填到path 。in query就需要构造了
if "'in': 'body'" in text:
if method == 'post':
req = requests.post(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
else:
req = requests.put(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
elif "'in': 'path'" in text:
param_map = {}
parameters = paths[path][method]['parameters']
for param in parameters:
p_type = ''
if "type" in param.keys():
p_type = param['type']
elif "schema" in param.keys():
if "type" in param["schema"].keys():
p_type = param['schema']['type']
p_name = param['name']
param_map[p_name] = payload_array[p_type]
if "{" in path:
tmps = re.findall("\{[^\}]*\}",path)
for tmp in tmps:
path = path.replace(tmp,param_map[tmp[1:-1]])
if method == 'post':
req = requests.post(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
else:
req = requests.put(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
elif "'in': 'query'" in text:
param_map = {}
parameters = paths[path][method]['parameters']
for param in parameters:
p_type = ''
if "type" in param.keys():
p_type = param['type']
elif "schema" in param.keys():
if "type" in param["schema"].keys():
p_type = param['schema']['type']
p_name = param['name']
param_map[p_name] = payload_array[p_type]
if method == 'post':
req = requests.post(url = domain + basePath + path , data = param_map,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,param_map,req.status_code,req.text]
else:
req = requests.put(url = domain + basePath + path , data = param_map,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,param_map,req.status_code,req.text]
else:#没有parameters这个key
if method == 'post':
req = requests.post(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
else:
req = requests.put(url = domain + basePath + path , data = json_payload,timeout = 5,verify = False)
hhh = [url,summary,path,method,domain + basePath + path,param_num,json_payload,req.status_code,req.text]
global_data.put(hhh)

elif method == "get" or method == "delete":
querystring = ""
param_map = {}
if "parameters" in paths[path][method].keys():#有参数
parameters = paths[path][method]['parameters']
for param in parameters:
p_type = ''
if "type" in param.keys():
p_type = param['type']
elif "schema" in param.keys():
if "type" in param["schema"].keys():
p_type = param['schema']['type']
p_in = param['in']
p_name = param['name']
try:
param_map[p_name] = payload_array[p_type]
except:
logger.error("参数类型不全,需要手动添加... => {}".format(p_type))
for key in param_map.keys():
querystring = querystring + key + "=" + param_map[key] + "&"
if "{" in path:
tmps = re.findall("\{[^\}]*\}",path)
for tmp in tmps:
path = path.replace(tmp,param_map[tmp[1:-1]]) #替换掉basePath里的{abc}
query_url = domain + basePath + path + '/?' + querystring[:-1]
if method == 'get':
req = requests.get(url = query_url,timeout = 5,verify = False)
hhh = [url,summary,path,method,query_url,param_num,param_map,req.status_code,req.text]
else:
req = requests.delete(url = query_url,timeout = 5,verify = False)
hhh = [url,summary,path,method,query_url,param_num,param_map,req.status_code,req.text]

else:#无参数
try:
query_url = domain + basePath + path
except Exception as e:
print(e)
if method == 'get':
req = requests.get(url = query_url,timeout = 5,verify = False)
hhh = [url,summary,path,method,query_url,param_num,param_map,req.status_code,req.text]
else:
req = requests.delete(url = query_url,timeout = 5,verify = False)
hhh = [url,summary,path,method,query_url,param_num,param_map,req.status_code,req.text]


global_data.put(hhh)

else:
logger.error("[!] 遇到了没有添加的请求方法...{}".format(method))
#exit()
except Exception as e:
logger.error(e)
except KeyboardInterrupt:
exit()
except Exception as e:
logger.error(e)


def go_html(urlq,q):
pass


def run(data):
url = data[0]
q = data[1]
url_type = check(url)
if url_type == 0:
logger.error("[!] Error")
exit()
elif url_type == 1:
logger.success("working on {}".format(url) ,"type: source")
go_source(url,q)
elif url_type == 2:
logger.success("working on {}".format(url) ,"type: api-docs")
go_docs(url,q)
else:
logger.success("working on {}".format(url) ,"type: html")
go_html(url,q)


def print_error(value):
print("进程池出错,出错原因为: ", value)

def run_pool(urls):
p = Pool(8)
manager = Manager()
q = manager.Queue()
for url in urls:
url = url.strip()
param = [url,q]
p.apply_async(run,args=(param,),error_callback=print_error)
p.close()
p.join()
output_to_csv(q)


def output_to_csv(global_data):
f = open('swagger.csv','w',newline='',encoding='utf-8')#写到csv中
writer = csv.writer(f)
try:
writer.writerow(["api-doc-url","summary","path","method","query_url","num of params","data","status_code","response"])
except Exception as e:
print(e)
while not global_data.empty():
writer.writerow(global_data.get())


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url", dest='target_url', help="resource地址 or api文档地址 or swagger首页地址")
parser.add_argument("-f", "--file", dest='url_file', help="批量测试")
args = parser.parse_args()


logger.add("file.log", format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}")
banner()
if args.target_url:
run_pool([args.target_url])
elif args.url_file:
f = open(args.url_file, 'r')
urls = f.readlines()
run_pool(urls)

测试单个Swagger中的所有API接口

1
python3 swagger-hack2.0.py -u http://example.com/swagger/doc.json

测试多个Swagger中的所有API接口

1
python3 swagger-hack2.0.py -f <file>

完成